package com.taptech.doufu.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.model.user.NovelBean
import com.taptech.doufu.ui.activity.story.StoryDetailsActivity
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.TagsUtil
import kotlinx.android.synthetic.main.adapter_story.view.*

class UserDetailStoryAdapter(context:Context): BaseRecyclerAdappter<NovelBean,UserDetailStoryAdapter.ViewHolder>(context){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.adapter_user_detail_story,null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        holder.itemView.tvTitle.text = item.topic_title
        holder.itemView.tvContent.text = item.description.replace("\\s+".toRegex(), "")
        holder.itemView.tvReadTime.text = item.tipTxt

        val tags = item.tags
        if (tags.isNotEmpty() && tags.size>0) {
            holder.itemView.tvTag1.visibility = View.VISIBLE
            holder.itemView.tvTag1.text = tags[0].name
            holder.itemView.tvTag1.setOnClickListener {
                TagsUtil.openWeexTagActivity(mContext,tags[0].name, Constant.OBJECT_TYPE_NOVEL,1)
            }
            if (tags.size > 1) {
                holder.itemView.tvTag2.visibility = View.VISIBLE
                holder.itemView.tvTag2.text = tags[1].name
                holder.itemView.tvTag2.setOnClickListener {
                    TagsUtil.openWeexTagActivity(mContext,tags[1].name, Constant.OBJECT_TYPE_NOVEL,1)
                }
            }else{
                holder.itemView.tvTag2.visibility = View.INVISIBLE
            }
        }else{
            holder.itemView.tvTag1.visibility = View.INVISIBLE
            holder.itemView.tvTag2.visibility = View.INVISIBLE
        }

        holder.itemView.setOnClickListener {
            StoryDetailsActivity.startActivity(mContext,item.id)
        }
    }

    class ViewHolder(view: View): RecyclerView.ViewHolder(view)
}